package cn.sugarscat.server;

import java.util.Scanner;
import cn.sugarscat.account.*;
import cn.sugarscat.admin.AdminManager;
import cn.sugarscat.flight.Flight;
import cn.sugarscat.flight.FlightManager;
import cn.sugarscat.flight.SeatManager;
import cn.sugarscat.order.OrderManager;
import cn.sugarscat.util.Module;

public class Server {
    public Server() {}

    public static void next() {
        Scanner scanner = new Scanner(System.in);
        // 在这里等待用户按下任意键
        System.out.print("按任意键继续···");
        scanner.nextLine(); // 等待用户输入任意字符
    }
    public static void start() {
        Scanner scanner = new Scanner(System.in);
        while (true) {
            // 顾客可以注册账户、登录和注销
            System.out.println("------------------------");
            System.out.println("-- 控制台版本航班订票系统 --");
            System.out.println("------------------------");
            System.out.println("1、注册");
            System.out.println("2、登录");
            System.out.println("3、退出");
            System.out.println("------------------------");
            System.out.print("请输入操作指令：");
            String command = scanner.next();
            switch (command) {
                case "1":
                register();
                    break;
                case "2":
                    login();
                    break;
                case "3":
                    System.out.println("退出成功！");
                    System.exit(0);
                    break;
                default:
                    System.out.println("输入有误，请重新输入！");
                    break;
            }
        }
    }

    // 注册
    private static void register() {
        Scanner scanner = new Scanner(System.in);
        User user = new User();
        System.out.println("------------------------");
        System.out.println("--------- 注 册 ---------");
        System.out.println("------------------------");
        System.out.print("请输入用户名：");
        user.setUname(scanner.next());
        System.out.println("检查用户名中···");
        if (AccountManager.checkUname(user.getUname())) {
            System.out.println("用户名已存在，请重新输入！");
            next();
            return;
        }
        System.out.print("请输入密码：");
        user.setPassword(scanner.next());
        System.out.print("请输入真实姓名：");
        user.setName(scanner.next());
        System.out.print("请输入电子邮件地址：");
        user.setEmail(scanner.next());
        System.out.print("请输入联系人信息：");
        user.setContactInfo(scanner.next());
        System.out.println("正在注册···");
        if (AccountManager.register(user)) {
            System.out.println("注册成功！");
        } else {
            System.out.println("注册失败，请重试！");
        }
        next();
    }

    // 登录
    private static void login() {
        Scanner scanner = new Scanner(System.in);
        while (true) {
            System.out.println("------------------------");
            System.out.println("--------- 登 录 ---------");
            System.out.println("------------------------");
            System.out.println("1、顾客登录");
            System.out.println("2、管理员登录");
            System.out.println("3、回退");
            System.out.println("------------------------");
            System.out.print("请输入操作指令：");
            String command = scanner.next();
            switch (command) {
                case "1":
                    System.out.print("请输入用户名：");
                    String username = scanner.next();
                    System.out.print("请输入密码：");
                    String password = scanner.next();
                    int uid = AccountManager.checkInfo(username, password);
                    if (uid >= 0) {
                        System.out.println("登录成功！");
                        mainMenu(uid);
                    } else {
                        System.out.println("用户名或密码错误，请重新登录！");
                        next();
                    }
                    break;
                case "2":
                    System.out.print("请输入用户名：");
                    String adminUsername = scanner.next();
                    System.out.print("请输入密码：");
                    String adminPassword = scanner.next();
                    if (AdminManager.checkInfo(adminUsername, adminPassword)) {
                        System.out.println("登录成功！");
                        adminMenu();
                    } else {
                        System.out.println("用户名或密码错误，请重新登录！");
                        next();
                    }
                    break;
                case "3":
                    return;
                default:
                    System.out.println("输入有误，请重新输入！");
                    break;
            }
        }
    }

    // 管理员主页
    private static void adminMenu() {
        Scanner scanner = new Scanner(System.in);
        while (true) {
            System.out.println("------------------------");
            System.out.println("------- 管理员主页 ------");
            System.out.println("------------------------");
            System.out.println("1、航班管理");
            System.out.println("2、顾客管理");
            System.out.println("3、退款查看");
            System.out.println("4、报告生成");
            System.out.println("5、退出系统");
            System.out.println("------------------------");
            System.out.print("请输入操作指令：");
            String command = scanner.next();
            switch (command) {
                case "1":
                    manageFlight();
                    break;
                case "2":
                    manageCustomer();
                    break;
                case "3":
                    refundView();
                    break;
                case "4":
                    System.out.print("请输入航班ID：");
                    int flightId = scanner.nextInt();
                    if (FlightManager.checkFlightId(flightId)) {
                        System.out.println("该航班不存在，请重新输入！");
                        return;
                    }
                    System.out.println("------------------------");
                    SeatManager.printSeatOccupancy(flightId);
                    System.out.println("------------------------");
                    next();
                    break;
                case "5":
                    System.out.println("退出成功！");
                    System.exit(0);
                    break;
                default:
                    System.out.println("输入有误，请重新输入！");
                    break;
            }
        }
    }

    private static void refundView() {
        System.out.println("------------------------");
        System.out.println("------ 已取消的订单 -----");
        System.out.println("------------------------");
        OrderManager.queryCancelOrders();
        System.out.println("------------------------");
        next();
    }

    private static void manageCustomer() {
        Scanner scanner = new Scanner(System.in);
        while (true) {
            System.out.println("------------------------");
            System.out.println("-------- 顾客管理 --------");
            System.out.println("------------------------");
            System.out.println("1、查询顾客信息");
            System.out.println("2、修改顾客信息");
            System.out.println("3、回到主页");
            System.out.println("------------------------");
            System.out.print("请输入操作指令：");
            String command = scanner.next();
            switch (command) {
                case "1":
                    getAllCustomer();
                    next();
                    break;
                case "2":
                    updateCustomer();
                    break;
                case "3":
                    return;
                default:
                    System.out.println("输入有误，请重新输入！");
                    break;
            }
        }
    }

    private static void getAllCustomer() {
        System.out.println("------------------------");
        System.out.println("-------- 顾客信息 --------");
        System.out.println("------------------------");
        AccountManager.printAllUserAccount();
        System.out.println("------------------------");
    }

    private static void updateCustomer() {
        Scanner scanner = new Scanner(System.in);
        System.out.print("请输入顾客ID:");
        int uid = scanner.nextInt();
        System.out.println("正在检测顾客是否存在···");
        if (!AccountManager.hasUser(uid)) {
            System.out.println("该顾客不存在，请重新输入！");
            next();
            return;
        }
        System.out.println("------------------------");
        System.out.println("------ 编辑顾客信息 ------");
        System.out.println("------------------------");
        System.out.println("1、修改用户名");
        System.out.println("2、修改密码");
        System.out.println("3、修改真实姓名");
        System.out.println("4、修改邮箱");
        System.out.println("5、修改联系人信息");
        System.out.println("6、退出编辑");
        System.out.println("------------------------");
        System.out.print("请输入操作指令：");
        doUpdateUserInfo(uid);
    }

    private static void manageFlight() {
        Scanner scanner = new Scanner(System.in);
        while (true) {
            System.out.println("------------------------");
            System.out.println("-------- 航班管理 --------");
            System.out.println("------------------------");
            System.out.println("1、查询航班信息");
            System.out.println("2、添加航班信息");
            System.out.println("3、修改航班信息");
            System.out.println("4、删除航班信息");
            System.out.println("5、退出管理");
            System.out.println("------------------------");
            System.out.print("请输入操作指令：");
            String command = scanner.next();
            switch (command) {
                case "1":
                    getAllFlight();
                    break;
                case "2":
                    addFlight();
                    break;
                case "3":
                    updateFlight();
                    break;
                case "4":
                    deleteFlight();
                    break;
                case "5":
                    return;
                default:
                    System.out.println("输入有误，请重新输入！");
                    break;
            }
            next();
        }
    }

    private static void deleteFlight() {
        Scanner scanner = new Scanner(System.in);
        System.out.print("请输入要删除的航班ID：");
        int flightId = scanner.nextInt();
        if (FlightManager.checkFlightId(flightId)) {
            System.out.println("该航班不存在，请重新输入！");
            return;
        }
        FlightManager.deleteFlight(flightId);
        System.out.println("删除成功！");
    }

    private static void getAllFlight() {
        System.out.println("------------------------");
        System.out.println("-------- 航班信息 --------");
        System.out.println("------------------------");
        FlightManager.printAllFlight();
        System.out.println("------------------------");
    }

    private static void addFlight() {
        Scanner scanner = new Scanner(System.in);
        Flight flight = new Flight();
        System.out.print("请输入航班号：");
        flight.setFlightNumber(scanner.next());
        System.out.println("正在检查航班号是否存在···");
        if (FlightManager.queryFlightNumber(flight.getFlightNumber()).getId()!=-1) {
            System.out.println("航班号已存在，请重新输入！");
            return;
        }
        System.out.print("请输入出发地：");
        flight.setDepartureLocation(scanner.next());
        System.out.print("请输入目的地：");
        flight.setDestinationLocation(scanner.next());
        String flightDepartureTime;
        while (true) {
            System.out.println("请输入出发时间(示例：2020-01-01-08:00:00)：");
            flightDepartureTime = scanner.next();
            if (Module.isCorrectTime(flightDepartureTime)) {
                break;
            } else {
                System.out.println("输入有误，请重新输入！");
            }
        }
        flight.setDepartureTime(flightDepartureTime);
        String flightArrivalTime;
        while (true) {
            System.out.println("请输入到达时间(示例：2020-01-01-08:00:00)：");
            flightArrivalTime = scanner.next();
            if (Module.isCorrectTime(flightArrivalTime)) {
                break;
            } else {
                System.out.println("输入有误，请重新输入！");
            }
        }
        flight.setArrivalTime(flightArrivalTime);
        System.out.print("请输入价格：");
        flight.setPrice(scanner.nextDouble());
        System.out.println("正在添加航班信息···");
        if (FlightManager.insertFlight(flight)){
            flight.setId(FlightManager.queryFlightNumber(flight.getFlightNumber()).getId());
            System.out.println("添加航班信息成功！");
            System.out.println("请继续添加座位数量···");
            next();
            FlightManager.printFlightSeatingMap();
            while (true) {
                System.out.print("请设置 A 区座位数量：");
                int seatANum = scanner.nextInt();
                if (seatANum>200) {
                    System.out.println("座位数量不能超过200!");
                    continue;
                }
                System.out.println("正在添加座位···");
                SeatManager.generateSeatingMap(flight.getId(), "A", seatANum);
                break;
            }
            while (true) {
                System.out.print("请设置 B 区座位数量：");
                int seatANum = scanner.nextInt();
                if (seatANum>500) {
                    System.out.println("座位数量不能超过500!");
                    continue;
                }
                System.out.println("正在添加座位···");
                SeatManager.generateSeatingMap(flight.getId(), "B", seatANum);
                break;
            }
            while (true) {
                System.out.print("请设置 C 区座位数量：");
                int seatANum = scanner.nextInt();
                if (seatANum>300) {
                    System.out.println("座位数量不能超过300!");
                    continue;
                }
                System.out.println("正在添加座位···");
                SeatManager.generateSeatingMap(flight.getId(), "C", seatANum);
                break;
            }
        } else {
            System.out.println("添加航班信息失败！");
        }
    }

    // 更新航班信息
    private static void updateFlight() {
        Scanner scanner = new Scanner(System.in);
        System.out.print("请输入要修改的航班ID：");
        int flightId = scanner.nextInt();
        if (FlightManager.checkFlightId(flightId)) {
            System.out.println("该航班不存在，请重新输入！");
            return;
        }
        System.out.println("------------------------");
        System.out.println("-------- 编辑航班 -------");
        System.out.println("------------------------");
        System.out.println("1、编辑航班号");
        System.out.println("2、编辑出发地");
        System.out.println("3、编辑目的地");
        System.out.println("4、编辑起飞时间");
        System.out.println("5、编辑到达时间");
        System.out.println("6、编辑票价");
        System.out.println("7、退出编辑");
        System.out.println("------------------------");
        System.out.print("请输入操作指令：");
        String command = scanner.next();
        switch (command) {
            case "1":
                System.out.print("请输入新的航班号：");
                String flightNumber = scanner.next();
                System.out.println("正在更新航班信息···");
                FlightManager.updateFlight(flightId, "flight_number", flightNumber);
                break;
            case "2":
                System.out.print("请输入新的出发地：");
                String flightDepartureLocation = scanner.next();
                System.out.println("正在更新航班信息···");
                FlightManager.updateFlight(flightId, "flight_departure_location", flightDepartureLocation);
                break;
            case "3":
                System.out.print("请输入新的目的地：");
                String flightDestinationLocation = scanner.next();
                System.out.println("正在更新航班信息···");
                FlightManager.updateFlight(flightId, "flight_destination_location", flightDestinationLocation);
                break;
            case "4":
                String flightDepartureTime;
                while (true) {
                    System.out.print("请输入新的起飞时间(示例：2020-01-01-08:00:00)：");
                    flightDepartureTime = scanner.next();
                    if (Module.isCorrectTime(flightDepartureTime)) {
                        break;
                    } else {
                        System.out.println("输入有误，请重新输入！");
                    }
                }
                System.out.println("正在更新航班信息···");
                FlightManager.updateFlight(flightId, "flight_departure_time", flightDepartureTime);
                break;
            case "5":
                String flightArrivalTime;
                while (true) {
                    System.out.print("请输入新的到达时间(示例：2020-01-01-08:00:00)：");
                    flightArrivalTime = scanner.next();
                    if (Module.isCorrectTime(flightArrivalTime)) {
                        break;
                    } else {
                        System.out.println("输入有误，请重新输入！");
                    }
                }
                System.out.println("正在更新航班信息···");
                FlightManager.updateFlight(flightId, "flight_arrival_time", flightArrivalTime);
                break;
            case "6":
                System.out.print("请输入新的票价：");
                double flightPrice = scanner.nextDouble();
                System.out.println("正在更新航班信息···");
                FlightManager.updateFlight(flightId, "flight_price", ""+flightPrice);
                break;
            case "7":
                System.out.println("退出编辑。");
                return;
            default:
                System.out.println("输入有误，请重新输入！");
                break;
        }
        next();
    }

    // 顾客主菜单
    private static void mainMenu(int uid) {
        Scanner scanner = new Scanner(System.in);
        while (true) {
            System.out.println("------------------------");
            System.out.println("-------- 主 菜 单 -------");
            System.out.println("------------------------");
            System.out.println("1、个人主页");
            System.out.println("2、机票订购");
            System.out.println("3、订单管理");
            System.out.println("4、退出系统");
            System.out.println("------------------------");
            System.out.print("请输入操作指令：");
            String command = scanner.next();
            switch (command) {
                case "1":
                    userHomePage(uid);
                    break;
                case "2":
                    bookTicketPage(uid);
                    break;
                case "3":
                    manageOrder(uid);
                    break;
                case "4":
                    System.out.println("退出成功！");
                    System.exit(0);
                    break;
                default:
                    System.out.println("输入有误，请重新输入！");
                    break;
            }
        }
    }

    // 用户主页
    private static void userHomePage(int uid) {
        Scanner scanner = new Scanner(System.in);
        while (true) {
            System.out.println("------------------------");
            System.out.println("-------- 个人主页 --------");
            System.out.println("------------------------");
            System.out.println("1、查看个人信息");
            System.out.println("2、修改个人信息");
            System.out.println("3、退出个人主页");
            System.out.println("------------------------");
            System.out.print("请输入操作指令：");
            String command = scanner.next();
            switch (command) {
                case "1":
                    AccountManager.printAccountInfo(uid);
                    System.out.println("------------------------");
                    next();
                    break;
                case "2":
                    updateUserInfo(uid);
                    break;
                case "3":
                    System.out.println("退出个人主页成功！");
                    return;
                default:
                    System.out.println("输入有误，请重新输入！");
                    break;
            }
        }
    }

    // 更新用户信息
    private static void updateUserInfo(int uid) {
        System.out.println("------------------------");
        System.out.println("------ 修改个人信息 ------");
        System.out.println("------------------------");
        System.out.println("1、修改用户名");
        System.out.println("2、修改密码");
        System.out.println("3、修改真实姓名");
        System.out.println("4、修改邮箱");
        System.out.println("5、修改联系人信息");
        System.out.println("6、退出修改");
        System.out.println("------------------------");
        System.out.print("请输入操作指令：");
        doUpdateUserInfo(uid);
    }

    private static void doUpdateUserInfo(int uid) {
        Scanner scanner = new Scanner(System.in);
        String command = scanner.next();
        switch (command) {
            case "1":
                System.out.print("请输入新的用户名：");
                String newUsername = scanner.next();
                System.out.println("检查用户名中···");
                if (AccountManager.checkUname(newUsername)) {
                    System.out.println("用户名已存在，请重新输入！");
                    next();
                    return;
                }
                System.out.println("正在更新用户信息···");
                AccountManager.updateAccountInfo(uid, "user_uname", newUsername);
                break;
            case "2":
                System.out.print("请输入新的密码：");
                String newPassword = scanner.next();
                System.out.println("正在更新用户信息···");
                AccountManager.updateAccountInfo(uid, "user_password", newPassword);
                break;
            case "3":
                System.out.print("请输入新的真实姓名：");
                String newName = scanner.next();
                System.out.println("正在更新用户信息···");
                AccountManager.updateAccountInfo(uid, "user_name", newName);
                break;
            case "4":
                System.out.print("请输入新的邮箱：");
                String newEmail = scanner.next();
                System.out.println("正在更新用户信息···");
                AccountManager.updateAccountInfo(uid, "user_email", newEmail);
                break;
            case "5":
                System.out.print("请输入新的联系人电话：");
                String newContactInfo = scanner.next();
                System.out.println("正在更新用户信息···");
                AccountManager.updateAccountInfo(uid, "user_contact_info", newContactInfo);
                break;
            case "6":
                System.out.println("退出修改。");
                return;
            default:
                System.out.println("输入有误，请重新输入！");
                break;
        }
        next();
    }

    private static void bookTicketPage(int uid) {
        Scanner scanner = new Scanner(System.in);
        while (true) {
            System.out.println("------------------------");
            System.out.println("-------- 机票订购 --------");
            System.out.println("------------------------");
            System.out.println("1、航班查询");
            System.out.println("2、预定机票");
            System.out.println("3、退出订购");
            System.out.println("------------------------");
            System.out.print("请输入操作指令：");
            String command = scanner.next();
            switch (command) {
                case "1":
                    queryFlight();
                    break;
                case "2":
                    bookTicket(uid);
                    break;
                case "3":
                    System.out.println("退出订购。");
                    return;
                default:
                    System.out.println("输入有误，请重新输入！");
                    break;
            }
        }
    }

    private static void queryFlight() {
        Scanner scanner = new Scanner(System.in);
        while (true) {
            System.out.println("------------------------");
            System.out.println("-------- 航班查询 --------");
            System.out.println("------------------------");
            System.out.println("1、根据‘出发地-目的地’查询");
            System.out.println("2、根据‘出发地-目的地-出发时间’查询");
            System.out.println("3、退出航班查询");
            System.out.println("------------------------");
            System.out.print("请输入操作指令：");
            String command = scanner.next();
            switch (command) {
                case "1":
                    System.out.print("请输入出发地：");
                    String from = scanner.next();
                    System.out.print("请输入目的地：");
                    String to = scanner.next();
                    System.out.println("正在查询航班信息···");
                    System.out.println("------------------------");
                    FlightManager.queryFlightsInfo(from, to);
                    System.out.println("------------------------");
                    break;
                case "2":
                    System.out.print("请输入出发地：");
                    String from2 = scanner.next();
                    System.out.print("请输入目的地：");
                    String to2 = scanner.next();
                    String time;
                    while (true) {
                        System.out.print("请输入出发时间(示例：2020-01-01-08:00:00)：");
                        time = scanner.next();
                        if (Module.isCorrectTime(time)) {
                            break;
                        } else {
                            System.out.println("输入有误，请重新输入！");
                        }
                    }
                    System.out.println("正在查询航班信息···");
                    System.out.println("------------------------");
                    FlightManager.queryFlightsInfo(from2, to2, time);
                    System.out.println("------------------------");
                    break;
                case "3":
                    System.out.println("退出航班查询。");
                    return;
                default:
                    System.out.println("输入有误，请重新输入！");
                    break;
            }
            next();
        }
    }

    private static void bookTicket(int uid) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("------------------------");
        System.out.println("-------- 预定机票 --------");
        System.out.println("------------------------");
        System.out.print("请输入航班号：");
        String flightNumber = scanner.next();
        System.out.println("正在查询航班···");
        if (FlightManager.queryFlightNumber(flightNumber).getId()==-1){
            System.out.println("无此航班！请重新选择。");
            next();
            return;
        }
        Flight flight = FlightManager.queryFlightNumber(flightNumber);
        if (OrderManager.queryOrderInfo(uid, flight.getId())!=-1){
            System.out.println("您已预定过该航班！请在订单管理中查看。");
            next();
            return;
        }
        if (flight.getId()!=-1) {
            FlightManager.bookTicket(uid, flight);
        } else {
            System.out.println("未找到该航班，请重新查询！");
        }
        next();
    }

    private static void manageOrder(int uid) {
        Scanner scanner = new Scanner(System.in);
        while (true) {
            System.out.println("------------------------");
            System.out.println("-------- 订单管理 --------");
            System.out.println("------------------------");
            System.out.println("1、查看订单");
            System.out.println("2、取消订单");
            System.out.println("3、回到主页");
            System.out.println("------------------------");
            System.out.print("请输入操作指令：");
            String command = scanner.next();
            switch (command) {
                case "1":
                    System.out.println("正在查询订单信息···");
                    System.out.println("------------------------");
                    OrderManager.queryOrders(uid);
                    System.out.println("------------------------");
                    break;
                case "2":
                    System.out.print("请输入要取消订单的ID：");
                    int id = scanner.nextInt();
                    System.out.println("取消订单中，请稍等···");
                    OrderManager.cancelOrder(id);
                    break;
                case "3":
                    System.out.println("回到主页面");
                    return;
                default:
                    System.out.println("输入有误，请重新输入！");
                    break;
            }
            next();
        }
    }
}
